package com.ideabove.radio.module;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.ideabove.adapter.ChannelAdapter;
import com.ideabove.adapter.CommentAdapter;
import com.ideabove.data.Channel;
import com.ideabove.data.Comment;
import com.ideabove.data.PlayItem;
import com.ideabove.network.NetWorkManager;
import com.ideabove.radio.MainActivity;
import com.ideabove.radio.R;
import com.ideabove.radio.service.PlayBackService;
import com.ideabove.radio.service.PlayBackService.MediaPlayerSingleton;
import com.ideabove.util.Connectivity;
import com.ideabove.util.img.ImageLoader;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("HandlerLeak")
public class ChannelMd{
	MainActivity activity 	= null;
	Thread getChannelListThd = null;
	ArrayList<Channel> channelArr = null;

	@SuppressLint("HandlerLeak")
	Handler mMainHandler = new Handler() {

		public void handleMessage(Message msg) {        	 
			//        	 LinearLayout channelList = (LinearLayout)activity.findViewById(R.id.list_channels);
			//        	 channelList.setVisibility(View.INVISIBLE);
			//        	 channelList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			//        	 LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//        	 										
			//        	 for(int i = 0; i < channelArr.size(); i++){
			//        		 View convertView = mInflater.inflate(R.layout.list_channel_row, null);	
			//        		 
			//        		 Channel channel = channelArr.get(i);
			//        		 TextView content = (TextView) convertView.findViewById(R.id.ch_desc);
			//    			 content.setText(channel.getDj_title());
			//    				
			//    			 ImageView imageView = (ImageView) convertView.findViewById(R.id.ch_image);
			//    			 ImageLoader.download(channel.getImg_path(), imageView);
			//    			 
			//    			 channelList.addView(convertView);
			//    			 
			//    			 convertView.setOnClickListener(new View.OnClickListener() {
			//					
			//					@Override
			//					public void onClick(View v) {
			//						// TODO Auto-generated method stub
			//						
			//					}
			//				});
			//        	 }
			//        	 channelList.setVisibility(View.VISIBLE);
		}

	};

	public void sendMessage(String msg){
		Message mssg = new Message();
		mssg.obj = msg;
		mMainHandler.sendMessage(mssg);
	}



	public void init(MainActivity activity){
		this.activity = activity;

		// Channel
		ImageView player_btn_play = (ImageView) activity.findViewById(R.id.img_genre_latin);
		player_btn_play.setOnClickListener(activity);
		LinearLayout radio_every_music = (LinearLayout) activity.findViewById(R.id.radio_every_music);
		radio_every_music.setOnClickListener(activity);		
		LinearLayout radio_ch_hotsong = (LinearLayout) activity.findViewById(R.id.radio_ch_hotsong);
		radio_ch_hotsong.setOnClickListener(activity);		
		LinearLayout radio_ch_kpophotsong = (LinearLayout) activity.findViewById(R.id.radio_ch_kpophotsong);
		radio_ch_kpophotsong.setOnClickListener(activity);		
		LinearLayout radio_ch_bonoflowfm = (LinearLayout) activity.findViewById(R.id.radio_ch_bonoflowfm);
		radio_ch_bonoflowfm.setOnClickListener(activity);		

		//		LinearLayout channels = (LinearLayout) activity.findViewById(R.id.list_channels);		
		//		channelArr = new ArrayList<Channel>();
		//		ListAdapter adapter = new ChannelAdapter(activity.getApplicationContext(),channelArr);	
		//		channels.setAdapter(adapter);


		//		getChannelListThd = new Thread(new Runnable() {
		//			public void run(){
		//				try {				
		//
		//					HttpClient client = NetWorkManager.getHttpConnection();    
		//					HttpConnectionParams.setConnectionTimeout(client.getParams(), 3000);										
		//					
		//					String postURL = "http://s.bonosound.com/bonosound_open_ver1/php/player/playpost.php";
		//					
		//			        HttpPost post = new HttpPost(postURL); 
		//			        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		//			        params.add(new BasicNameValuePair("q", "djlist"));
		//			        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
		//			        post.setEntity(ent);
		//			        HttpResponse responsePOST = client.execute(post);  
		//			        
		//					InputStream in = responsePOST.getEntity().getContent();
		//					BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		//					StringBuilder str = new StringBuilder();
		//					String line = null;
		//					while((line = reader.readLine()) != null)
		//					{					
		//						str.append(line);
		//					}
		//					in.close();					
		//					String jsonStr = str.toString();
		//										
		//			        if(jsonStr != null && jsonStr.length() > 0){			
		//							JSONObject jsonObj = new JSONObject(jsonStr); 
		//							JSONArray resultArray = jsonObj.getJSONArray("result");
		//			
		//							Log.e("####", "####" + jsonObj);
		//							//"dj_id": "1",
		//							//"user_id": "11",
		//							//"dj_title": "\ubaa8\ub4e0 \ubcf4\ub178\uc0ac\uc6b4\ub354\ub4e4\uc758 \ub2e4\uc591\ud55c \uace1\uc744 \ub79c\ub364\uc73c\ub85c \ub9cc\ub098\ubcf4\uc138\uc694.",
		//							//"play_cnt": "0",
		//							//"img_file": "http:\/\/s.bonosound.com\/bono_user_imgs\/dj_01.jpg",
		//							//"update_time": "2012-10-09 11:24:16",
		//							//"type": "1",
		//							//"is_visible": "1",
		//							//"folder_id": null
		//							//]}
		//							
		//							for (int i = 0; i < resultArray.length(); i++) {
		//								Channel channel = new Channel();
		//								JSONObject jObj = resultArray.getJSONObject(i);
		//								channel.setDj_id(jObj.getString("dj_id").toString());
		//								channel.setDj_title(jObj.getString("dj_title").toString());
		//								channel.setFolder_id(jObj.getString("folder_id").toString());
		//								channel.setImg_path(jObj.getString("img_file").toString());
		//								channel.setIs_visible(jObj.getString("is_visible").toString());
		//								channel.setPlay_cnt(jObj.getString("play_cnt").toString());
		//								channel.setType(jObj.getString("type").toString());
		//								channel.setUpdate_time(jObj.getString("update_time").toString());
		//								channel.setUser_id(jObj.getString("user_id").toString());
		//								
		//								channelArr.add(channel);			
		//
		//							}
		//							
		//							sendMessage("get_chlist_done");						
		//					}
		//
		//					
		//				}catch(Exception e){
		//					getChannelListThd.destroy();
		//					getChannelListThd.start();
		//					e.printStackTrace();
		//				}
		//				getChannelListThd = null;	
		//			}
		//		});
		//
		//		getChannelListThd.start();
	}



	public void onClick(View v,Messenger mMessenger,Messenger mService ){

		if(!Connectivity.isConnected(activity.getApplicationContext())){
			Toast.makeText(activity.getApplicationContext(), "Network is not available.", Toast.LENGTH_SHORT ).show();
			return;
		}

		MediaPlayerSingleton.whatKindOfNetwork = Connectivity.getWhatKindOfNetwork(activity.getApplicationContext());		
		Message msg = Message.obtain(null, PlayBackService.REQUEST_MSG_GET_BONO_CH_PLAYLIST);
		try{
			switch(v.getId()){
			case R.id.radio_every_music:					
				msg.replyTo = mMessenger;
				msg.arg1 = 1; // type
				mService.send(msg);				
				break;

			case R.id.radio_ch_hotsong:
				msg.replyTo = mMessenger;
				msg.arg1 = 2; // type
				mService.send(msg);	
				break;
			case R.id.radio_ch_kpophotsong:
				msg.replyTo = mMessenger;
				msg.arg1 = 3; // type
				mService.send(msg);
				break;
			case R.id.radio_ch_bonoflowfm:
				msg.replyTo = mMessenger;
				msg.arg1 = 4; // type
				mService.send(msg);
				break;

			}		
		}catch(Exception e){
			e.printStackTrace();
		}
	}


//	public void showGenreList(){
//
//		if(mOrientation  == Configuration.ORIENTATION_PORTRAIT){
//			studio_layout.setVisibility(View.VISIBLE);
//		}
//	}
//
//	public void toggleGenreList(){
//		if(studio_layout != null){
//			if(studio_layout.getVisibility() == View.VISIBLE){
//				studio_layout.setVisibility(View.GONE);
//			}else{
//				studio_layout.setVisibility(View.VISIBLE);
//			}
//		}		
//	}
}
